package com.jsnu.xiaochi.controller;

import com.alibaba.fastjson.JSON;
import com.jsnu.xiaochi.pojo.*;
import com.jsnu.xiaochi.service.AdminService;
import com.jsnu.xiaochi.service.DetailService;
import com.jsnu.xiaochi.service.MyService;
import com.jsnu.xiaochi.util.GetUUID;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
public class DetailController {
    @Resource
    DetailService detailService;
    @Resource
    AdminService adminService;
    @Resource
    MyService myService;
    @RequestMapping("detail")
    public String showDetail(Model model, HttpServletRequest request, String foodId){
        model.addAttribute("food",detailService.selectFoodById(foodId));
        User user = (User) request.getSession().getAttribute("user");
        if(user!=null){
            String username = user.getUsername();
            model.addAttribute("like",detailService.selectFavorite(username,foodId).size());
        }
        return "detail";
    }

    @RequestMapping("insertFavorite")
    @ResponseBody
    public String insertFavorite(String username,String foodid){
        System.out.println(username+" "+foodid);
        Favorite favorite=new Favorite();
        favorite.setFoodId(foodid);
        favorite.setUsername(username);
        detailService.insertFavorite(favorite);
        return "success";
    }
    @RequestMapping("deleteFavorite")
    @ResponseBody
    public String deleteFavorite(String username,String foodid){
        System.out.println(username+" "+foodid);
        detailService.deleteFavorite(username,foodid);
        return "success";
    }

    @RequestMapping("insertComment")
    @ResponseBody
    public String insertComment(String score,String comment,String username,String foodid){
//        生成评论信息
        Random random = new Random();
        char prefix= (char) (random.nextInt(26)+'A');
        String comment_id=prefix + GetUUID.getUUID().substring(10,19);
        Comments comments = new Comments();
        comments.setUsername(username);
        comments.setComment(comment);
        comments.setScore(score);
        comments.setId(comment_id);
        comments.setCreateTime(new Timestamp(System.currentTimeMillis()));
        detailService.insertComment(comments);


//        通过id获取小吃
        Food myFood = detailService.selectFoodById(foodid);
        int count=myFood.getCommentCount();
        Food food =new Food();
        food.setId(foodid);
        food.setCommentCount(count+1);
        double Score = Double.parseDouble(myFood.getScore());
        double Count = count;
        double TotalScore=(Score*Count+Integer.parseInt(score))/(Count+1);
        food.setScore(String.valueOf(TotalScore).substring(0,3));
        adminService.updateFood(food);

//        生成关联表信息
        Food_comments food_comments=new Food_comments();
        food_comments.setFoodId(foodid);
        food_comments.setCommentsId(comment_id);
        detailService.insertFoodComment(food_comments);

        return "success";
    }

    @RequestMapping("getAllComments")
    @ResponseBody
    public String getAllComments(String foodId){
        Map map = new HashMap<>();
        List<Comments> comments=detailService.getAllComments(foodId);
        map.put("data",comments);
        return JSON.toJSONString(map);
    }
}
